/**
 *  Copyright 2009-2010 Steven Ottenhoff (Dual-IT)
 * 
 *  This file is part of Clazzified.
 *
 *  Clazzified is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Clazzified is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Clazzified.  If not, see http://www.gnu.org/licenses/gpl.
 * 
 */

package nl.dualit.clazzified.core.model.attribute;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Observable;
import java.util.Observer;

import nl.dualit.clazzified.core.model.attribute.constraint.IConstraint;
import nl.dualit.clazzified.core.model.clazz.Clazz;
import nl.dualit.clazzified.store.clazz.AttributeFields;

public abstract class AbstractAttribute extends Observable implements IAttribute{
	private Long id;
	private String name;
	private String description;
	private AttributeTypes type;
	private Clazz clazz;
	private List<IConstraint> constraints;
	
	/**
	 * Returns an instance of a new IAttribute.
	 * 
	 * @param clazz
	 * @param name
	 * @param description
	 * @param typeName
	 */
	AbstractAttribute( Clazz clazz, Long attributeId, String name, String description, 
			List<IConstraint> constraints ){
		
		this.id = attributeId;
		this.clazz = clazz;
		this.name = name;
		this.description = description;
		this.constraints = constraints;
		
	}
		
	@Override
	public String getName() {
		return name;
	}

	@Override
	public void setName(String name) {
		this.name = name;
		setChanged();
		notifyObservers( name );
	}

	@Override
	public String getDescription() {
		return description;
	}

	@Override
	public void setDescription(String description) {
		this.description = description;
		setChanged();
		notifyObservers( description );
	}


	@Override
	public void removeConstraint(IConstraint constraint) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Long getId() {
		return id;
	}

	@Override
	public void setId(Long id) {
		this.id = id;
	}

	@Override
	public List<IConstraint> getConstraints() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void removeObserver(Observer o) {
		// TODO Auto-generated method stub
		super.deleteObserver( o );
	}
	
	@Override
	public Map<AttributeFields,String> getDataMap(){
		
		Map<AttributeFields,String> map = new HashMap<AttributeFields,String>();
		if( id != null )
			map.put( AttributeFields.ATTRIBUTE_ID, id.toString() );
		map.put( AttributeFields.ATTRIBUTE_NAME, name );
		map.put( AttributeFields.ATTRIBUTE_DESCRIPTION, description );
		map.put( AttributeFields.TYPE_NAME, type.typeName() );
		map.put( AttributeFields.CLAZZ_ID, clazz.getId().toString() );
		
		return map;
	}
	
	protected void setAbstractConstraints( List<IConstraint> constraintList ){
		this.constraints = constraintList;
	}

	 
	protected List<IConstraint> getAbstractConstraints(){
		
		if( constraints == null )
			constraints = new ArrayList<IConstraint>();
		
		return constraints;
	}
	
	
}
